package a02;

import java.awt.BasicStroke;
import java.awt.Color;

public class GoBoard<M> extends RegularBoard<M> {

    private static final long serialVersionUID = 1L;
    private double screenHeight = java.awt.Toolkit.getDefaultToolkit()
	    .getScreenSize().getHeight();
    int[][] units = { { 50, 60, 70 }, { 35, 45, 55 }, { 25, 35, 40 } };
    int[][] dotX = { { 2, 4, 6, 6, 2 }, { 2, 6, 10, 2, 10 },
	    									{ 3, 9, 15, 3, 15 ,9,3,15,9} };
    int[][] dotY = { { 2, 4, 6, 2, 6 }, { 2, 6, 10, 10, 2 },
	    									{ 3, 9, 15, 15, 3 ,3,9,9,15} };

    int mode;

    public GoBoard(RegularGame<M> game) {
	super(game);
	mode = getGame().getRows() > 18 ? 2 : getGame().getRows() > 12 ? 1 : 0;

    }

    @Override
    public int getUNIT() {
	return screenHeight < 901 ? (screenHeight < 900 ? units[mode][0]
		: units[mode][1]) : units[mode][2];
    }    
 

    @Override
    public void paintStone(byte c, byte r) {
	super.getTheGraphics().setColor(
		this.selectColor(getGame().getAtPosition(c, r)));
	super.getTheGraphics().fillOval((c * this.getUNIT()) + 5,
		(r * this.getUNIT()) + 5, (this.getUNIT()) - 10,
		(this.getUNIT()) - 10);
	if (!(getLastMove()==null) && c == getLastMove().fst && r == getLastMove().snd) {
	    super.getTheGraphics().setColor(Color.GREEN);
	    super.getTheGraphics().fillOval((c * this.getUNIT()) + ((this.getUNIT()/2)-(this.getUNIT()/10)),
			(r * this.getUNIT()) + ((this.getUNIT()/2)-(this.getUNIT()/9)), (this.getUNIT()) - ((this.getUNIT()/2)+(this.getUNIT()/3)),
			(this.getUNIT()) - ((this.getUNIT()/2)+(this.getUNIT()/3)));
	}
    }

    @Override
    public void paintGrid(int gridMode, Color gridColor) {
	int shifting = 0;
	byte plus = 0;
	if (gridMode == 2) {
	    shifting = (getUNIT() / 2);
	    plus = 1;
	}
	for (byte r = (byte) (1 - plus); r < (getGame().getRows()); r++) {
	    super.getTheGraphics().setColor(gridColor);
	    super.getTheGraphics().setStroke(
		    new BasicStroke(2 - plus, BasicStroke.CAP_SQUARE,
			    BasicStroke.JOIN_MITER));
	    super.getTheGraphics().drawLine(
		    0 + shifting,
		    (r * this.getUNIT()) + shifting,
		    ((getGame().getColumns() - plus) * this.getUNIT())
			    + shifting, (r * this.getUNIT()) + shifting);
	}

	for (byte c = (byte) (1 - plus); c < (getGame().getColumns()); c++) {
	    super.getTheGraphics().setStroke(
		    new BasicStroke(2 - plus, BasicStroke.CAP_SQUARE,
			    BasicStroke.JOIN_MITER));
	    super.getTheGraphics().setColor(gridColor);
	    super.getTheGraphics().drawLine((c * this.getUNIT()) + shifting,
		    (0 + shifting), (c * this.getUNIT()) + shifting,
		    ((getGame().getRows() - plus) * this.getUNIT()) + shifting);
	}

	for (int i = 0; i < 3; i++) {
	    if (mode == i) {
		for (int j = 0; j < dotX[i].length; j++) {
		    super.getTheGraphics().fillOval(
			    (dotX[i][j] * this.getUNIT())
				    + (this.getUNIT() / 2 - 4),
			    (dotY[i][j] * this.getUNIT())
				    + (this.getUNIT() / 2 - 4), (9), (9));

		}
	    }
	}
    }

}
